﻿using Assets.Core.Common.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;
using Nucleic.Web.Models.Invest;

namespace Nucleic.Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class InvestController : ControllerBase
    {
        /// <summary>
        /// 获取流调模板
        /// </summary>
        /// <returns></returns>
        [Route("GetAllEpideinves")]
        [HttpPost]
        public Result<List<v_epideinves>> GetAllEpideinves()
        {
            var items = SqlSugarFactory.Instance.Queryable<v_epideinves>().OrderBy(x => x.seq).ToList();
            return Result<List<v_epideinves>>.success(items);
        }
        /// <summary>
        /// 流调表保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Route("Save")]
        [HttpPost]
        public Result Save(InvestSaveInput input)
        {
            var dt = DateTime.Now;
            var inves_id = SqlSugarFactory.Instance.Insertable<patient_invest>(new patient_invest
            {
                pat_id = input.pat_id,
                ins_time = dt,
            }).ExecuteReturnIdentity();

            foreach (var item in input.epide)
            {
                SqlSugarFactory.Instance.Insertable<patient_invest_sub>(new patient_invest_sub
                {
                    inves_id = inves_id,
                    epide_id = item.epide_id,
                    epide_values = item.epide_values,
                    memo = item.memo
                }).ExecuteCommand();
            }
            return Result.success(inves_id);
        }
    }
}
